package com.maoxx.humanresourcemanage_service.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.maoxx.humanresourcemanage_service.dto.Response;
import com.maoxx.humanresourcemanage_service.dto.ResponseDTO;
import com.maoxx.humanresourcemanage_service.entity.Staff;
import com.maoxx.humanresourcemanage_service.enums.BusinessStatusEnum;
import com.maoxx.humanresourcemanage_service.mapper.StaffMapper;
import com.maoxx.humanresourcemanage_service.utils.JWTUtil;
import com.maoxx.humanresourcemanage_service.utils.MD5Util;
import com.maoxx.humanresourcemanage_service.vo.StaffDeptVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class LoginService extends ServiceImpl<StaffMapper, Staff> {

    @Resource
    private StaffMapper staffMapper;

    public ResponseDTO login(Staff staff) {
        // 使用MD5加密
        String password = MD5Util.MD55(staff.getPassword());
        StaffDeptVO staffDeptVO = staffMapper.findStaffInfo(staff.getCode(), password);

        if (staffDeptVO != null) {
            // 验证用户的状态
            if (staffDeptVO.getStatus() == 1) {
                // 生成token，并返回token
                String token = JWTUtil.generateToken(staffDeptVO.getId(), password);
                System.out.println("token:" + token);
                return Response.success(staffDeptVO, token);
            }
            // 如果存在该用户，但是状态为禁用，那么就联系管理员
            return Response.error(BusinessStatusEnum.STAFF_STATUS_ERROR);
        }
        // 如果没找到该用户，就证明用户名或密码错误
        return Response.error("用户名或密码错误！");
    }
}
